import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'model.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

void gallery(List<GalleryModel> items, {int showIndex = 0}) {
  Get.to(
    () => GalleryPage(
      items: items,
      showIndex: showIndex,
    ),
    transition: Transition.noTransition,
  );
}

class GalleryPage extends StatelessWidget {
  GalleryPage({Key? key, required this.items, required this.showIndex})
      : super(key: key) {
    pageCtrl = PageController(initialPage: showIndex);
  }
  final List<GalleryModel> items;
  final int showIndex;
  late PageController pageCtrl;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        PhotoViewGallery.builder(
            itemCount: items.length,
            pageController: PageController(initialPage: showIndex),
            builder: (ctx, index) {
              return PhotoViewGalleryPageOptions(
                imageProvider: NetworkImage(items[index].url),
                heroAttributes: PhotoViewHeroAttributes(
                  tag: items[index].id,
                ),
              );
            }),
        Positioned(
          left: 0,
          child: SafeArea(
            child: InkWell(
              onTap: () {
                pageCtrl.dispose();
                Get.back();
              },
              child: Container(
                width: 40.r,
                height: 40.r,
                alignment: Alignment.center,
                child: const Icon(
                  Icons.close,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
